package com.zhuoyue.biz.core.command.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zhuoyue.biz.core.api.dao.*;
import com.zhuoyue.biz.core.api.domain.LocalSceneDO;
import com.zhuoyue.biz.core.api.domain.TblDeviceActions;
import com.zhuoyue.biz.core.api.domain.TblPasswordShare;
import com.zhuoyue.biz.core.api.domain.ZigbeeDeviceProperty;
import com.zhuoyue.biz.core.command.service.CmdSqlHandleService;
import com.zhuoyue.common.exception.ErrorType;
import com.zhuoyue.common.exception.ZyServerException;
import com.zhuoyue.common.utils.CmdUtils;
import com.zhuoyue.common.utils.Gb2312;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class CmdSqlHandleServiceImpl implements CmdSqlHandleService {
    private static final int SCENE_CODE_10 = 10;

    private final TblDeviceActionsMapper deviceActionsMapper;
    private final LocalSceneDao localSceneDao;
    private final TblPasswordShareMapper passwordShareMapper;
    private final ZigbeeDevicePropertyMapper zigbeeDevicePropertyMapper;

    @Override
    public void modifyOneNightLightTime(String deviceId, Map<String, Object> actions) {
        String motorTimeParameter = actions.get("motorTimeParameter") == null ? null : (String) actions.get("motorTimeParameter");
        if (StringUtils.isNotEmpty(motorTimeParameter)) {
            motorTimeParameter = CmdUtils.intToHexTwo(Integer.parseInt(motorTimeParameter));
            TblDeviceActions deviceActions = new TblDeviceActions();
            deviceActions.setActionsKey("motorTimeParameter");
            deviceActions.setActionsValue(motorTimeParameter);
            int count = deviceActionsMapper.update(deviceActions, Wrappers.<TblDeviceActions>lambdaQuery()
                    .eq(TblDeviceActions::getDeviceId, deviceId).eq(TblDeviceActions::getActionsKey, "motorTimeParameter"));
            if (count < 1) {
                TblDeviceActions deviceActions1 = TblDeviceActions.builder().actionsKey("motorTimeParameter").actionsValue(motorTimeParameter).deviceId(deviceId).build();
                count = deviceActionsMapper.insert(deviceActions1);
                if (count < 1) {
                    throw new ZyServerException(ErrorType.Service_Fail);
                }
            }
        }

    }

    @Override
    public List<TblDeviceActions> getActionByDeviceId(String id) {
        List<TblDeviceActions> deviceActions = deviceActionsMapper.selectByDeviceId(id);
        return deviceActions;
    }

    @Override
    public String getSceneCode(String sceneId) {
        Integer code = localSceneDao.selectById(sceneId).getCode();
        if (code < SCENE_CODE_10) {
            return "0" + (code + "");
        }
        return code + "";
    }

    @Override
    public List<String> getSceneCodeAndName(String sceneId) throws UnsupportedEncodingException {
        List<String> list = new ArrayList<>(2);
        LocalSceneDO localSceneDO = localSceneDao.selectById(sceneId);
        Integer code = localSceneDO.getCode();
        String nameStr = localSceneDO.getName();
        if (code < SCENE_CODE_10) {
            list.add(0, "0" + (code + ""));
        }
        if (StringUtils.isNotEmpty(nameStr)) {
            list.add(1, Gb2312.chineseToGb(nameStr));
        }

        return list;
    }

    @Override
    public void setPassword(TblPasswordShare passwordShare) {
        int insert = passwordShareMapper.insert(passwordShare);
        if (insert < 1) {
            throw new ZyServerException(ErrorType.OBJECT_NOT_FOUND.getCode(), "分享密码保存失败");
        }
    }

    @Override
    public void deletePassword(TblPasswordShare passwordShare) {
        passwordShareMapper.delete(Wrappers.<TblPasswordShare>lambdaQuery()
                .eq(TblPasswordShare::getDeviceMac, passwordShare.getDeviceMac()).eq(TblPasswordShare::getType, passwordShare.getType()));
    }

    @Override
    public void updatePassWord(TblPasswordShare passwordShare) {
        passwordShareMapper.update(passwordShare, Wrappers.<TblPasswordShare>lambdaQuery()
                .eq(TblPasswordShare::getDeviceMac, passwordShare.getDeviceMac())
                .eq(TblPasswordShare::getType, passwordShare.getType()));
    }

    @Override
    public ZigbeeDeviceProperty getDeviceProperty(String id) {
        return zigbeeDevicePropertyMapper.selectById(id);
    }

}
